<template>
  <!-- 第三方配置-对接配置 -->
  <div class="unit_information">
    <div class="header_title">
      <span>对接配置</span>
    </div>
    <div class="list">
      <el-form
        ref="alldata"
        :model="alldata"
        :rules="alldatarule"
        :label-position="labelPosition"
        label-width="160px"
        :disabled="!isauth('third_sestting')"
      >
        <el-row>
          <el-col :span="13">
            <el-form-item label=" 第三方对接地址:" prop="url">
              <el-input v-model="alldata.url" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="对接凭证:" prop="token">
              <el-col :span="12">
                <el-input v-model="alldata.token" size="mini" disabled></el-input>
              </el-col>
              <el-col :span="6">
                <el-button type="primary" @click="onToken()" size="mini" class="token">生成凭证</el-button>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送部门信息:">
              <el-switch
                v-model="alldata.pushDepartmentData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送部门信息路径:" prop="pushDepartmentUrl">
              <el-input v-model="alldata.pushDepartmentUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送人事信息:">
              <el-switch
                v-model="alldata.pushUserData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送人事信息路径:" prop="pushUserUrl">
              <el-input v-model="alldata.pushUserUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送消费数据:">
              <el-switch
                v-model="alldata.pushConsumeData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送消费信息路径:" prop="pushConsumeUrl">
              <el-input v-model="alldata.pushConsumeUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送水控数据:">
              <el-switch
                v-model="alldata.pushWaterData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送水控信息路径:" prop="pushWaterUrl">
              <el-input v-model="alldata.pushWaterUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送门锁数据:">
              <el-switch
                v-model="alldata.pushLockData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label=" 推送门锁信息路径:" prop="pushLockUrl">
              <el-input v-model="alldata.pushLockUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送门禁数据:">
              <el-switch
                v-model="alldata.pushEntranceData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label=" 推送门禁信息路径:" prop="pushEntranceUrl">
              <el-input v-model="alldata.pushEntranceUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否推送充值信息:">
              <el-switch
                v-model="alldata.pushRechargeData"
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label=" 推送充值信息路径:" prop="pushRechargeUrl">
              <el-input v-model="alldata.pushRechargeUrl" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <el-button type="primary" @click="onSubmit()">保存信息</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userGuid: '',
      labelPosition: 'right',
      alldata: {
        url: '',
        token: '',
        /**
         * 是否推送人事信息（包括部门）
         */
        pushUserData: '0',

        /**
         * 是否推送消费数据
         */
        pushConsumeData: '0',

        /**
         * 是否推送水控数据
         */
        pushWaterData: '0',

        /**
         * 是否推送门锁数据
         */
        pushLockData: '0',

        /**
         * 是否推送门禁数据
         */
        pushEntranceData: '0',

        /**
         * 是否推送充值信息
         */

        pushRechargeData: '0',
        /**
         * 是否推送部门信息
         */
        pushDepartmentData: '0',
        // 推送信息路径
        pushConsumeUrl: '',
        pushDepartmentUrl: '',
        pushEntranceUrl: '',
        pushLockUrl: '',
        pushRechargeUrl: '',
        pushUserUrl: '',
        pushWaterUrl: ''
      },
      alldatarule: {
        url: [
          {
            required: true,
            message: '第三方对接地址不能为空',
            trigger: 'blur'
          }
        ],
        token: [
          { required: true, message: '对接凭证不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.userGuid = sessionStorage.getItem('administrator')
  },
  mounted() {
    // 查询对接配置
    this.getuintdata()
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  methods: {
    onToken() {
      this.$set(this.alldata, 'token', this.guid())
    },
    guid() {
      return 'xxxxxxxx-xxxx-xxxy-xxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(
        c
      ) {
        var r = (Math.random() * 16) | 0,
          v = c == 'x' ? r : (r & 0x3) | 0x8
        return v.toString(16)
      })
    },
    // 查询对接配置
    getuintdata() {
      console.log(this.guid())
      console.log(this.guid().length)
      this.$http({
        url: this.$http.adornUrl('/unit/unit-auth-token-search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: this.changeschoolid,
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.alldata =
            data.messageContent == null ? this.alldata : data.messageContent
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 修改
    onSubmit() {
      this.alldata.unitGuid = this.changeschoolid
      this.$refs['alldata'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/unit/unit-auth-token-update'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: this.alldata,
              messageType: this.messageType
            })
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                showClose: true,
                message: '配置成功',
                type: 'success'
              })
              this.getuintdata()
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.unit_information {
  padding: 10px;
  /deep/.el-col-12,.el-col-8,.el-col-24 {
    height: 40px;
  }
  .token {
    margin-left: 20px;
  }
  .header_title {
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #e5e9f2;
    color: #909399;
  }
  .list {
    font-size: 16px;
    font-weight: bold;
    color: #909399;
    .el-form-item {
      padding-left: 22px;
      // height: 50px;
      line-height: 50px;
      margin-bottom: 0px;
      // padding-top: 18px;
      // border-bottom: 1px solid #e5e9f2;
      width: 100%;
    }
    .el-form-item:last-child {
      border-bottom: 0px solid #e5e9f2;
      margin-top: 30px;
    }
  }
}
</style>